package com.zyn.inventory.management.entities;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import javax.persistence.*;

import java.util.*;

/**
 * A User is a person in the Knappsack system. This entity contains relevant
 * information pertaining to the person.
 * 
 * @see UserDetails
 */
@Entity
@Table(name = "user")
// @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class User extends BaseEntity implements UserDetails {

	private static final long serialVersionUID = 1690650481252062307L;

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "ID")
	private Long id;

	@Column(name = "username", unique = true)
	private String username;

	@Column(name = "password")
	private String password;

	@Column(name = "email")
	private String email;

	@Column(name = "real_name")
	private String realName;
	
	@Column(name = "activated")
    private boolean activated = false;
	
	public User(String username,String password,String email,String realName,boolean passwordExpired,List<Role> roles){
		this.username = username ;
		this.password = password;
		this.email = email;
		this.realName = realName;
		this.activated = true;
		this.passwordExpired = passwordExpired;
		this.roles = roles;
	}

	public boolean isActivated() {
		return activated;
	}

	public void setActivated(boolean activated) {
		this.activated = activated;
	}

	@ManyToMany(cascade = CascadeType.REFRESH,fetch=FetchType.EAGER)
	@JoinTable(name = "USER_ROLE", joinColumns = @JoinColumn(name = "USER_ID"), inverseJoinColumns = @JoinColumn(name = "ROLE_ID"))
	private List<Role> roles = new ArrayList<Role>();

	@Column(name = "PASSWORD_EXPIRED")
	private boolean passwordExpired = false;

	public String getRealName() {
		return realName;
	}

	public void setRealName(String realName) {
		this.realName = realName;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public User() {
		this.username = "";
		this.password = "";
		this.email = "";
	}

	public Long getId() {
		return id;
	}

	public List<Role> getRoles() {
		return roles;
	}

	public void setRoles(List<Role> roles) {
		this.roles = roles;
	}

	@Transient
	public Collection<GrantedAuthority> getAuthorities() {
		Collection<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();

		for (Role role : getRoles()) {
			authorities.add(new SimpleGrantedAuthority(role.getAuthority()));
		}

		return authorities;
	}

	public String getEmail() {
		return email;
	}

	public String getPassword() {
		return password;
	}

	public String getUsername() {
		return username;
	}

	public boolean isAccountNonExpired() {
		return true;
	}

	public boolean isAccountNonLocked() {
		return true;
	}

	public boolean isCredentialsNonExpired() {
		return true;
	}

	public boolean isEnabled() {
		return true;
	}

	public void setUsername(String username) {
		this.username = username.toLowerCase().trim();
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setEmail(String email) {
		this.email = email.toLowerCase().trim();
	}

	public boolean isPasswordExpired() {
		return passwordExpired;
	}

	public void setPasswordExpired(boolean passwordExpired) {
		this.passwordExpired = passwordExpired;
	}

	public boolean isSystemAdmin() {
		// TODO Auto-generated method stub
		return false;
	}
}
